package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.accessory.impl;

import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.accessory.OrganizationMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.accessory.IamOrganizationVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.accessory.IamOrganizationService;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Organization;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Ztree;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.OutEmployeeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.OutOrganizationService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangsy on 2017/2/27.
 */
public class IamOrganizationServiceImpl implements IamOrganizationService
{
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(IamOrganizationServiceImpl.class);

    @Autowired(required = false)
    private OutOrganizationService outOrganizationService;

    @Autowired(required = false)
    private OutEmployeeService outEmployeeService;
    @Autowired
    private OrganizationMapper organizationMapper;

    @Override
    public Organization organization(String userId) {
        Employee employee = employee(userId);
        Organization organization = new Organization();
        if(employee == null ){
            return organization;
        }
        organization.setOrgId(employee.getOrgId());
        return organization;
    }

    @Override
    public Employee employee(String userId){
        Employee employee = null;
        if(StringUtils.isNotEmpty(userId)){
            try {
                employee = outEmployeeService.findById(userId);
            }catch (Exception e){
                logger.error("获取当前人信息异常:"+e.getMessage());
            }
        }
        return employee;
    }

    @Override
    public Organization getWorkZone(String userId) {
        Employee employee = employee(userId);
        List<Organization>  organizationList = new ArrayList<>();
        try {
            organizationList = outOrganizationService.getOrganizationList(employee);
        }catch (Exception e){
            logger.error("获取当前人所属作业区异常:"+ e.getMessage());
        }
        Organization organization = null;
        if(organizationList != null && organizationList.size() == 1){
            organization = organizationList.get(0);
        }
        return organization;
    }

    @Override
    public List<Ztree> loadOrgZtree(String orgId, boolean sync, boolean openOrg, boolean openDept, String keyword) {
        List<Ztree>  ztreeList =  null;
        try {
            ztreeList = outEmployeeService.loadEmployeeZtree(orgId,true,openOrg,openDept,keyword);
        }catch (Exception e){
            logger.error("调用组织部门树异常:"+e.getMessage());
        }
        return ztreeList;
    }

    @Override
    public List<Ztree> listOrg(Organization organization) {
        List<Ztree>  ztreeList =  null;
        try {
            ztreeList = outOrganizationService.queryOrgTree(organization);
        }catch (Exception e){
            logger.error("调用组织部门树异常:"+e.getMessage());
        }
        return ztreeList;
    }

    @Override
    public List<Ztree>getOrganizationList(Employee employee, String maxLevel, String lessLevel) {
        List<Ztree>  ztreeList =  null;
        List<Organization> list = null;
        try {
            list = outOrganizationService.getOrganizationList(employee, maxLevel, lessLevel);
            System.out.println("组织》》》"+list);
        } catch (Exception e) {
            logger.error("获取人员组织信息失败:" + e.getMessage());
        }
        return ztreeList;
    }

    /**
     * 根据 组织id查询直属作业区/若orgId为气矿，则查询气矿下所有作业区
     * @param orgId 组织id
     * @return
     */
    @Override
    public List<IamOrganizationVo> getZyqByOrgId(String orgId){

        List<IamOrganizationVo> list = new ArrayList<>();
        // orgId为空则返回null
        if(StringUtils.isEmpty(orgId)){
            return null;
        }
        // 向上查找作业区
        IamOrganizationVo organization = organizationMapper.getUpZyqByOrgId(orgId);
        if(organization != null){
            list.add(organization);
            return  list;
        }
        // 向下查找作业区
        List<IamOrganizationVo> organizationList = organizationMapper.getDownZyqByOrgId(orgId);

        return  organizationList;
    }

    /**
     * 根据orgId查询查询以此为父节点的组织
     * @param orgId 组织id
     * @return
     */
    @Override
    public List<IamOrganizationVo> getNextOrganizationListByOrgId(String orgId){
        List<IamOrganizationVo> list = organizationMapper.getNextOrganizationListByOrgId(orgId);
        return  list;
    }
}
